package com.hj.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hj.form.StuRoleForm;
import com.hj.pojo.Student;
import com.hj.service.IStudentService;
import com.hj.vo.PageResult;
import com.hj.vo.Result;

@RestController
@RequestMapping(value = "/stu")
@CrossOrigin
public class StudentController {

    @Autowired
    IStudentService studentService;
    @Autowired
    HttpServletResponse response;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page
    		, @RequestParam(defaultValue = "3") Integer size) {

        PageInfo<Student> pageInfo = studentService.list(page, size);
    	PageResult pr = new PageResult();
    	pr.setData(pageInfo.getList());
    	pr.setRow(pageInfo.getTotal());
        
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        result.setData(pr);

        return result;
    }

    @GetMapping("/findOne/{id}")
    @ResponseBody
    public Result findOne(@PathVariable("id") String id) throws Exception {
        Student stu = studentService.findOne(id);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        result.setData(stu);
        return result;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Student student) {
    	Result result = new Result();
    	studentService.add(student);
		result.setCode(200);
		result.setMsg("ok");
        return result;
        
    }
    
    @PostMapping("/role/add")
    public Result addRole(@RequestBody StuRoleForm stuRole) {
    	Result result = new Result();
    	studentService.addRoles(stuRole);
    	result.setCode(200);
    	result.setMsg("ok");
    	return result;
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }

    @DeleteMapping("/delete/{id}")
    public Result del(@PathVariable("id") String id) {
        studentService.delete(id);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }

}
